package com.xindacloud.sms.console.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SmsAgentGroupDTO;
import com.xindacloud.sms.console.system.entity.SmsAgent;
import com.xindacloud.sms.console.system.entity.SmsAgentGroup;
import com.xindacloud.sms.console.system.entity.SmsPricing;
import com.xindacloud.sms.console.system.mapper.SmsAgentGroupMapper;
import com.xindacloud.sms.console.system.service.ISmsAgentGroupService;
import com.xindacloud.sms.console.system.service.ISmsAgentService;
import com.xindacloud.sms.console.system.service.ISmsPricingService;
import com.xindacloud.sms.console.system.service.ISmsUserAllowOperatorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SmsAgentGroupServiceImpl extends ServiceImpl<SmsAgentGroupMapper, SmsAgentGroup> implements ISmsAgentGroupService {

    @Autowired
    ISmsAgentService smsAgentService;
    @Autowired
    ISmsPricingService pricingService;
    @Autowired
    ISmsUserAllowOperatorService userAllowOperatorService;

    @Override
    public SmsAgentGroup queryOneAgentInfoByName(String groupName) {
        return lambdaQuery().eq(SmsAgentGroup::getStatus, true)
                .eq(SmsAgentGroup::getGroupName, groupName)
                .one();
    }

    @Override
    public SmsAgentGroup queryOneAgentInfoById(String groupId) {
        return lambdaQuery().eq(SmsAgentGroup::getStatus, true)
                .eq(SmsAgentGroup::getGroupId, groupId)
                .one();
    }

    @Override
    public SmsAgentGroup saveSmsAgentGroup(SmsAgentGroupDTO smsAgentGroupDTO) {
        final SmsAgentGroup agentGroup = SmsAgentGroup.builder()
                .groupName(smsAgentGroupDTO.getGroupName())
                .status(smsAgentGroupDTO.getStatus())
                .routeId(smsAgentGroupDTO.getRouteId())
                .build();
        save(agentGroup);
        return agentGroup;
    }

    @Override
    public List<SmsAgentGroup> querySmsAgentGroupList() {
        return lambdaQuery()
                //.eq(SmsAgentGroup::getStatus, true)
                .list();
    }


    @Override
    public Boolean updateAgentGroupStatus(String agentGroupId, Boolean status) {
        try {
            //更新通道组状态
            lambdaUpdate()
                    .set(SmsAgentGroup::getStatus, status)
                    .eq(SmsAgentGroup::getGroupId, agentGroupId)
                    .update();
            //更新通道状态
            final List<String> agentGroupIds = Arrays.asList(agentGroupId);
            smsAgentService.updateAgentStatus(agentGroupIds, status);
            List<SmsAgent> agentList = smsAgentService.querySmsAgentByGroupId(agentGroupIds);
            if (CollectionUtils.isNotEmpty(agentList)) {
                //更新运营商列表
                final List<String> agentIds = agentList.stream()
                        .map(SmsAgent::getAgentId)
                        .collect(Collectors.toList());
                pricingService.updateStatusByAgentIds(agentIds, status);
                if (CollectionUtils.isNotEmpty(agentIds)) {
                    //更新用户运营商通道
                    final List<SmsPricing> pricingList = pricingService.querySmsPricing(agentIds);
                    if (CollectionUtils.isNotEmpty(pricingList)) {
                        final List<String> operatorList = pricingList.stream()
                                .map(SmsPricing::getOperatorId)
                                .collect(Collectors.toList());
                        userAllowOperatorService.updateStatusByOperatorIds(operatorList, status);
                    }
                }

            }
            return true;
        } catch (Exception e) {
            log.error("级联更新通道组异常:{}", e.getMessage(), e);
            return false;
        }

    }

	@Override
	public Result deleteAgentGroups(SmsAgentGroupDTO smsAgentGroupDTO) {
		if(this.removeByIds(smsAgentGroupDTO.getAgentGroupIds())) {
			return ResultGenerator.genSuccessResult("删除成功！" );
		}else {
			return ResultGenerator.genServerFailResult("删除失败！请联系管理员");
		}
	}
}
